//
//  DKWelcomeViewController.swift
//  Sina
//
//  Created by 赵柏清 on 16/7/31.
//  Copyright © 2016年 DarkZhao. All rights reserved.
//

import UIKit
import SDWebImage

class DKWelcomeViewController: UIViewController {
    
    override func loadView() {
        view = backgroundImage
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupUI()
        
    }
    
    override func viewDidAppear(animated: Bool) {
        setAnimation()
    }
    

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    private func setupUI() {
        view.addSubview(iconImageView)
        view.addSubview(tipsLabel)
        
        // MARK: 设置控件的约束
        iconImageView.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(view.snp_centerX)
            make.bottom.equalTo(view.snp_bottom).offset(-200)
            
        }
        
        tipsLabel.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(view.snp_centerX)
            make.top.equalTo(iconImageView.snp_bottom).offset(20)
            
        }
        
    }
    
    // MARK: 设置弹簧动画
    private func setAnimation() {
        
        /*
        usingSpringWithDamping: 弹簧效果 0~1.0 取值越小,弹簧效果越明显
        initialSpringVelocity:表示初始的速度
        OC里给枚举的默认值是 填写 0;swift中 用 []标示
        */
        
        // 更新我们想要的约束
        iconImageView.snp_updateConstraints { (make) -> Void in
            make.bottom.equalTo(self.view.snp_bottom).offset(-UIScreen.mainScreen().bounds.height + 200)
        }
        UIView.animateWithDuration(2.0, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 10, options: [], animations: { () -> Void in
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                // 在此页面跳转
                NSNotificationCenter.defaultCenter().postNotificationName(kNotificationChangeViewController, object: self)
        }
        
    }
    
/// 视图背景图片
    private lazy var backgroundImage :UIImageView = {
        
        let backImageView = UIImageView(image: UIImage(named: "ad_background"))
        
        return backImageView
    }()
/// 头像图片
    let Lmodel = DKLoginModel.readModel()
    private lazy var iconImageView : UIImageView = {
        
        // 先设置占位图
        let icon = UIImageView(image: UIImage(named: "avatar_default_big"))
        
        // 通过网络加载用户头像
        if let OURLString = self.Lmodel?.profile_image_url {
            
            let URL = NSURL(string: OURLString)
            
            icon.sd_setImageWithURL(URL)
           
            icon.layer.cornerRadius = 100.0 / 4.0
        } else {
            // 设置圆角以及边框样式
            icon.layer.cornerRadius = 170.0 / 4.0
        }
        
        icon.layer.masksToBounds = true
    
        icon.layer.borderWidth = 2
        icon.layer.borderColor = UIColor.yellowColor().CGColor
        
        
        
        return icon
    }()
/// 欢迎文字
    private lazy var tipsLabel : UILabel = {
        let tips = UILabel()
        tips.text = "欢迎归来"
        tips.font = UIFont.systemFontOfSize(16)
        tips.textAlignment = .Center
        tips.textColor = UIColor.darkGrayColor()
        return tips
    }()

}
